package mashibing.class3;

import pojo.DoubleNode;
import pojo.Node;

/**
 * 链表反转
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-03-10
 */
public class Class3_1_Reverse {

    public static Node reverseNode(Node node) {
        Node pre = null;
        Node next = null;
        while (node != null) {
            next = node.next;
            node.next = pre;
            pre = node;
            node = next;
        }
        return pre;
    }

    public static DoubleNode reverseDoubleNode(DoubleNode doubleNode) {
        DoubleNode pre = null;
        DoubleNode next = null;
        while (doubleNode != null) {
            next = doubleNode.next;
            doubleNode.next = pre;
            doubleNode.last = next;
            pre = doubleNode;
            doubleNode = next;
        }
        return pre;
    }

    public static void main(String[] args) {
//        Node node = new Node(1, new Node(2, new Node(3, null)));
//        System.out.println(node);
//        Node reverseNode = reverseNode(node);
//        System.out.println(reverseNode);

//        DoubleNode doubleNode1 = new DoubleNode(1);
//        DoubleNode doubleNode2 = new DoubleNode(2);
//        DoubleNode doubleNode3 = new DoubleNode(3);
//        doubleNode1.last = null;
//        doubleNode1.next = doubleNode2;
//        doubleNode2.last = doubleNode1;
//        doubleNode2.next = doubleNode3;
//        doubleNode3.last = doubleNode2;
//        doubleNode3.next = null;
//        DoubleNode temp = doubleNode1;
//        while (temp != null) {
//            System.out.print(temp.value + ",");
//            temp = temp.next;
//        }
//        System.out.println();
//        temp = reverseDoubleNode(doubleNode1);
//        while (temp != null) {
//            System.out.print(temp.value + ",");
//            temp = temp.next;
//        }
    }
}
